package com.phonegap.gwt.childbrowser.client.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.phonegap.gwt.childbrowser.client.*;

public class ChildBrowserImpl {
	
	
	  public native void showWebPage(ChildBrowser childBrowser,String url) /*-{
		 	childBrowser.showWebPage(url,true);
	  }-*/;
	  


	  
	  
	  public native ChildBrowser getChildBrowser() /*-{
	  		var cb = $wnd.ChildBrowser.install();
	   		return cb;
	  }-*/;
	  
	  
	  public native void setOpenExternalCallback(ChildBrowser childBrowser,
			  OnOpenExternalCallback callback) /*-{
	  	childBrowser.onOpenExternal =  function() {
      @com.phonegap.gwt.childbrowser.client.impl.ChildBrowserImpl::handleOnOpenExternal(Lcom/phonegap/gwt/childbrowser/client/OnOpenExternalCallback;) (callback);
    };
}-*/;

	  
	  //cb.onClose = function(){root.onCloseBrowser()};
	  public native void setOnCloseCallback(ChildBrowser childBrowser,
			  OnCloseCallback callback) /*-{
		  	childBrowser.onClose =  function() {
		        @com.phonegap.gwt.childbrowser.client.impl.ChildBrowserImpl::handleOnClose(Lcom/phonegap/gwt/childbrowser/client/OnCloseCallback;) (callback);
		      };
	  }-*/;

	  

	  public native void setOnLocationChangeCallback(ChildBrowser childBrowser,
		      OnLocationChangeCallback callback)  /*-{
	  	childBrowser.onLocationChange =  function() {
      @com.phonegap.gwt.childbrowser.client.impl.ChildBrowserImpl::handleOnLocationChange(Lcom/phonegap/gwt/childbrowser/client/OnLocationChangeCallback;Ljava/lang/String;) (callback,location);
    };
}-*/;
	  

	  
	  

	  
	  
	  protected static final void handleOnClose(OnCloseCallback callback) {
		 	
		    UncaughtExceptionHandler ueh = GWT.getUncaughtExceptionHandler();
	      try {
	    	  callback.browserClosed();	
		      } catch (Throwable t) {
		        ueh.onUncaughtException(t);
		        
		      }
	  }
	  protected static final void handleOnOpenExternal(OnOpenExternalCallback callback) {
		 	
		    UncaughtExceptionHandler ueh = GWT.getUncaughtExceptionHandler();
	      try {
	    	  callback.onOpenExternal();	
		      } catch (Throwable t) {
		        ueh.onUncaughtException(t);
		        
		      }
	  }
	  
	  protected static final void handleOnLocationChange(OnLocationChangeCallback callback,String location) {
		 	
		    UncaughtExceptionHandler ueh = GWT.getUncaughtExceptionHandler();
	      try {
	    	  callback.locChanged(location);
		      } catch (Throwable t) {
		        ueh.onUncaughtException(t);
		        
		      }
	  }
	  
	  
	  

		  
}
